//格式化时间
function dateFtt(fmt,date)   
{ //author: meizz   
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
} 
function formatSubscribeTime(cellValue, options, rowObject) {
	var dateStr = "";
	if(cellValue){
		var crtTime = new Date(cellValue);
		dateStr = dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);
	}
    return dateStr;
}
//上报类型字典
function typeFormate(value, options, row){
    //sex为自定义字典项
    var statusObj = vm.typeObject.reportType;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}
//上报状态字典
function stateFormate(value, options, row){
    //sex为自定义字典项
	var statusObj = vm.stateObject.reportState;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}

//查看详情,打开标签
function reportInfoDetail(reportInfoId){
    //先关闭菜单实现刷新效果
    parent.closeTabByPageId("999901");
    //id为菜单ID，请从菜单管理中查找，如非配置的菜单则设定个比较大的数为ID，以免与系统菜单冲突。
    parent.addTabs({id:'999901',title: '查看详情',close: true,url: 'modules/recorderReport/reportinfoDetail.html?reportInfoId='+reportInfoId ,urlType: 'relative'});
}

//审核
function reportInfoAudit(id){
    console.log("iddddddd------"+id);

    confirm("确认要审核吗？", function () {

        if(id == null){
            return ;
        }
        $.get(baseURL + "recorderReport/reportinfo/reportFinanceAudit/"+id, function(r){
            if(r.code === 0){
                alert('操作成功', function(index){
                    vm.reload();
                });
            }else{
                alert(r.msg);
            }
        });

    })
}

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'recorderReport/reportinfo/listReportFinanceAuditInfo',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id_', width: 50, key: true, hidden: true },
			{ label: '上报时间', name: 'time', index: 'time', width: 80 , formatter: formatSubscribeTime },
			{ label: '上报类型', name: 'type', index: 'type', width: 80, formatter:typeFormate },
			{ label: '上报地点', name: 'address', index: 'address', width: 80 },
			{ label: '上报用户', name: 'userName', index: 'user_name', width: 80 },
			{ label: '上报人车牌', name: 'plateNumber', index: 'plate_number', width: 80 },
			{ label: '联系方式', name: 'phone', index: 'phone', width: 80 },
			{ label: 'IMEI号', name: 'imei', index: 'imei', width: 80 },
            { label: '状态', name: 'state', index: 'state', width: 80, formatter:stateFormate },
            { label: '审核时间', name: 'createTime', index: 'create_time', width: 80 , formatter:formatSubscribeTime},
            { label: '详情', name: '', index: '',width: 50,
                formatter: function (cellvalue, option, rowObject) {
                    return '&nbsp;&nbsp;<button class="btn btn-info btn-xs" type="button" onclick=reportInfoDetail(\"' + rowObject.id + '\")>详情</button>';

                }
            },
            { label: '审核', name: '', index: '',width: 50,
                formatter: function (cellvalue, option, rowObject) {
                    return '&nbsp;&nbsp;<button class="btn btn-xs" style="background-color: #ee9200 ; color: #ffffff" type="button" onclick=reportInfoAudit(\"' + rowObject.id + '\")>审核</button>';

                }
            }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        multiboxonly: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			type:'',
			state:'',
			address:'',
			userName:''
		},
        typeObject:{}, //类型集合
		stateObject:{}, //状态集合

        showList: true,
		title: null,
		reportInfo: {}
	},
    mounted:function(){
        this.$nextTick(function(){ this.initSysDict()});
    },
	methods: {
        initSysDict:function() {
            $.get(baseURL + "sys/dict/select/reportType", function (r) {
                if (r.code === 0) {
                    vm.typeObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
            $.get(baseURL + "sys/dict/select/reportState", function (r) {
                if (r.code === 0) {
                    vm.stateObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
        },
        exportExcel: function () {
            location.href = baseURL+'recorderReport/reportinfo/exportReportFinanceAudit';
        },
		query: function () {
			vm.reload();
		},
        reset: function(){
            vm.q.type = '';
            vm.q.state = '';
            vm.q.address = '';
            vm.q.userName = '';
        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.reportInfo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.reportInfo.id == null ? "recorderReport/reportinfo/save" : "recorderReport/reportinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.reportInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "recorderReport/reportinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "recorderReport/reportinfo/info/"+id, function(r){
                vm.reportInfo = r.reportInfo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
			    postData:{
                    'type': vm.q.type,
                    'state': vm.q.state,
                    'address': vm.q.address,
                    'userName': vm.q.userName
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});